package Hot100.Medium.LinkedList;

public class LC19_DeleteNodeFromLast {
    public static void main(String[] args) {

    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        int len = 0;
        ListNode dummy = new ListNode();
        dummy.next = head;
        ListNode cur = head;
        while(cur != null){
            len++;
            cur = cur.next;
        }
        cur = dummy;
        int position = len - n;
        for(int i = 0; i < position; i++){
            cur = cur.next;
        }
        ListNode temp = cur.next.next;
        cur.next = temp;
        return dummy.next;
    }
}
